package leetcode;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 描述:46. 全排列
 *
 * @author Madison You
 * @created 23:19
 */
public class Mid_46_Permute {

    public static void main(String[] args) {
        int[] ints = {1,1,3};
        new Mid_46_Permute().permute(ints);
    }

    List<List<Integer>> list = new ArrayList<>();
    List<Integer> temp = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        boolean[] flag = new boolean[nums.length];
        back(nums, flag);
        return list;
    }

    public void back(int[] arr,boolean[] flag) {
        if (temp.size() == arr.length) {
            list.add(new ArrayList<>(temp));
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            if (i > 0 && arr[i] == arr[i - 1] && !flag[i-1]) {
                continue;
            }
            if (!flag[i]) {
                temp.add(arr[i]);
                flag[i] = true;
                back(arr,flag);
                flag[i] = false;
                temp.remove(temp.size() - 1);
            }

        }
    }

}
